package test4;

import java.util.Arrays;
import java.util.Scanner;

public class Solution2 {
    public static void main(String[] args) {// 计数排序
        System.out.println("请输入需要排序的数字个数：");
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[] arr = new int[n];
        System.out.println("请输入需要排序的数字：");
        for (int i = 0; i < n; i++) {
            arr[i] = sc.nextInt();
        }
        int[] res = CountingSort(arr);
        System.out.println(Arrays.toString(res));
    }

    private static int[] CountingSort(int[] arr) {
        int[] count = new int[arr.length];
        int[] res = new int[arr.length];
        for (int i = 0; i < arr.length; i++) {
            for (int j = i + 1; j < count.length; j++) {
                if (arr[i] < arr[j]){
                    count[j]++;
                }else{
                    count[i]++;
                }
            }
        }
        for (int i = 0; i < arr.length; i++) {
            res[count[i]] = arr[i];
        }
        return res;
    }
}
